import axios from "axios";
import { Message } from "element-ui";
import NproGress from 'nprogress'
import 'nprogress/nprogress.css'
import WhiteList from './list.json'

// 白名单
// let WhiteList = ["/user/login"]
// let WhiteList = [
//     {
//         methods:'post',
//         url:'/user/login'
//     }
// ]

// 请求拦截
axios.interceptors.request.use((config) => {
    // 开启加载中
    NproGress.start()
    // console.log(config);
    const token = sessionStorage.getItem('token') ? sessionStorage.getItem('token') : '';
    config.headers.Authorization = token
    console.log(token);
    return config
})

// 响应拦截
axios.interceptors.response.use((data) => {
    // 关闭加载过程
    NproGress.done()
    // 判断当前地址是否存在白名单中
    let flag = WhiteList.includes(data.config.url)
    if(flag){
        Message.success(data.data.msg)
    }

    return data.data
})

export default axios